<?php

namespace Ecmpay\Ecmpay\ecmpay;

use Ecmpay\Ecmpay\Base;

class wxpay extends Base
{
    /**
     * 微信Native支付
     * @param array $orderData
     * @param string $notify_url
     * @param string $return_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4699165483036275
     */
    public static function wxpayForNative(array $orderData, string $notify_url = null)
    {
        if (!isset($orderData['trade_no']) || empty($orderData['trade_no'])) {
            throw new \Exception("缺少商户单号: trade_no");
        }
        if (!isset($orderData['total_amount']) || empty($orderData['total_amount'])) {
            throw new \Exception("缺少金额: total_amount");
        }
        if (!isset($orderData['subject']) || empty($orderData['subject'])) {
            throw new \Exception("缺少订单标题: subject");
        }
        if (!isset(self::$config['app_id']) || empty(self::$config['app_id'])) {
            throw new \Exception("初始化配置中缺少应用APPID: app_id");
        }
        $apiUrl = self::$ecmUrl.'wxpay/nativePay';
        $postData['app_id'] = self::$config['app_id']; //应用APPID
        $postData['trade_no'] = $orderData['trade_no'];
        $postData['total_amount'] = $orderData['total_amount'];
        $postData['subject'] = $orderData['subject'];
        if (isset($orderData['sub_mchid'])) {
            $postData['sub_mchid'] = $orderData['sub_mchid'];
        }
        if (isset($orderData['mch_no'])) {
            $postData['mch_no'] = $orderData['mch_no'];
        }
        $postData['notify_url'] = $notify_url;
        $postData['sign'] = self::$lib::createSign($postData,self::$config['appSecret']);
        $result = self::$lib::httpRequest($apiUrl, $postData);
        $result2 = json_decode($result['data'], true);
        if (empty($result2) || !isset($result2['code']) || $result2['code'] != 200) {
            throw new \Exception($result2['msg']);
        } else {
            return $result2['data'];
        }
    }
    /**
     * 微信JSAPI支付
     * @param array $orderData
     * @param string $notify_url
     * @param string $return_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4699165483036275
     */
    public static function wxpayForJsApi(array $orderData, string $notify_url = null)
    {
        if (!isset($orderData['trade_no']) || empty($orderData['trade_no'])) {
            throw new \Exception("缺少商户单号: trade_no");
        }
        if (!isset($orderData['total_amount']) || empty($orderData['total_amount'])) {
            throw new \Exception("缺少金额: total_amount");
        }
        if (!isset($orderData['subject']) || empty($orderData['subject'])) {
            throw new \Exception("缺少订单标题: subject");
        }
        if (!isset(self::$config['app_id']) || empty(self::$config['app_id'])) {
            throw new \Exception("初始化配置中缺少应用APPID: app_id");
        }
        $apiUrl = self::$ecmUrl.'wxpay/jsApiPay';
        $postData['app_id'] = self::$config['app_id']; //应用APPID
        $postData['trade_no'] = $orderData['trade_no'];
        $postData['total_amount'] = $orderData['total_amount'];
        $postData['subject'] = $orderData['subject'];
        if (isset($orderData['sub_mchid'])) {
            $postData['sub_mchid'] = $orderData['sub_mchid'];
        }
        if (isset($orderData['mch_no'])) {
            $postData['mch_no'] = $orderData['mch_no'];
        }
        if (isset($orderData['sub_appid'])) {
            $postData['sub_appid'] = $orderData['sub_appid'];
        }
        if (isset($orderData['attach'])) {
            $postData['attach'] = $orderData['attach'];
        }
        if (isset($orderData['goods_detail'])) {
            $postData['goods_detail'] = $orderData['goods_detail'];
        }
        if (isset($orderData['time_expire'])) {
            $postData['time_expire'] = $orderData['time_expire'];
        }
        if (isset($orderData['invoice_id'])) {
            $postData['invoice_id'] = $orderData['invoice_id'];
        }
        if (isset($orderData['goods_tag'])) {
            $postData['goods_tag'] = $orderData['goods_tag'];
        }
        if (isset($orderData['sp_openid'])) {
            $postData['sp_openid'] = $orderData['sp_openid'];
        }
        if (isset($orderData['sub_openid'])) {
            $postData['sub_openid'] = $orderData['sub_openid'];
        }
        if (isset($orderData['profit_sharing'])) {
            $postData['profit_sharing'] = $orderData['profit_sharing'];
        }
        if (isset($orderData['cost_price'])) {
            $postData['cost_price'] = $orderData['cost_price'];
        }
        if (isset($orderData['payer_client_ip'])) {
            $postData['payer_client_ip'] = $orderData['payer_client_ip'];
        }
        if (isset($orderData['device_id'])) {
            $postData['device_id'] = $orderData['device_id'];
        }
        if (isset($orderData['store_info'])) {
            $postData['store_info'] = $orderData['store_info'];
        }
        $postData['notify_url'] = $notify_url;
        $postData['sign'] = self::$lib::createSign($postData,self::$config['appSecret']);
        $result = self::$lib::httpRequest($apiUrl, $postData);
        $result2 = json_decode($result['data'], true);
        if (empty($result2) || !isset($result2['code']) || $result2['code'] != 200) {
            throw new \Exception($result2['msg']);
        } else {
            return $result2['data'];
        }
    }
    /**
     * 微信APP支付
     * @param array $orderData
     * @param string $notify_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4699138781635476
     */
    public static function wxpayForApp(array $orderData, string $notify_url = null)
    {
        if (!isset($orderData['trade_no']) || empty($orderData['trade_no'])) {
            throw new \Exception("缺少商户单号: trade_no");
        }
        if (!isset($orderData['total_amount']) || empty($orderData['total_amount'])) {
            throw new \Exception("缺少金额: total_amount");
        }
        if (!isset($orderData['subject']) || empty($orderData['subject'])) {
            throw new \Exception("缺少订单标题: subject");
        }
        if (!isset(self::$config['app_id']) || empty(self::$config['app_id'])) {
            throw new \Exception("初始化配置中缺少应用APPID: app_id");
        }
        $apiUrl = self::$ecmUrl.'wxpay/appPay';
        $postData['app_id'] = self::$config['app_id']; //应用APPID
        $postData['trade_no'] = $orderData['trade_no'];
        $postData['total_amount'] = $orderData['total_amount'];
        $postData['subject'] = $orderData['subject'];
        if (isset($orderData['sub_mchid'])) {
            $postData['sub_mchid'] = $orderData['sub_mchid'];
        }
        if (isset($orderData['mch_no'])) {
            $postData['mch_no'] = $orderData['mch_no'];
        }
        if (isset($orderData['sub_appid'])) {
            $postData['sub_appid'] = $orderData['sub_appid'];
        }
        if (isset($orderData['attach'])) {
            $postData['attach'] = $orderData['attach'];
        }
        if (isset($orderData['goods_detail'])) {
            $postData['goods_detail'] = $orderData['goods_detail'];
        }
        if (isset($orderData['time_expire'])) {
            $postData['time_expire'] = $orderData['time_expire'];
        }
        if (isset($orderData['invoice_id'])) {
            $postData['invoice_id'] = $orderData['invoice_id'];
        }
        if (isset($orderData['goods_tag'])) {
            $postData['goods_tag'] = $orderData['goods_tag'];
        }
        if (isset($orderData['sp_openid'])) {
            $postData['sp_openid'] = $orderData['sp_openid'];
        }
        if (isset($orderData['sub_openid'])) {
            $postData['sub_openid'] = $orderData['sub_openid'];
        }
        if (isset($orderData['profit_sharing'])) {
            $postData['profit_sharing'] = $orderData['profit_sharing'];
        }
        if (isset($orderData['cost_price'])) {
            $postData['cost_price'] = $orderData['cost_price'];
        }
        if (isset($orderData['payer_client_ip'])) {
            $postData['payer_client_ip'] = $orderData['payer_client_ip'];
        }
        if (isset($orderData['device_id'])) {
            $postData['device_id'] = $orderData['device_id'];
        }
        if (isset($orderData['store_info'])) {
            $postData['store_info'] = $orderData['store_info'];
        }
        $postData['notify_url'] = $notify_url;
        $postData['sign'] = self::$lib::createSign($postData,self::$config['appSecret']);
        $result = self::$lib::httpRequest($apiUrl, $postData);
        $result2 = json_decode($result['data'], true);
        if (empty($result2) || !isset($result2['code']) || $result2['code'] != 200) {
            throw new \Exception($result2['msg']);
        } else {
            return $result2['data'];
        }
    }
    /**
     * 微信H5支付
     * @param array $orderData
     * @param string $notify_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4699162923109473
     */
    public static function wxpayForH5(array $orderData, string $notify_url = null)
    {
        if (!isset($orderData['trade_no']) || empty($orderData['trade_no'])) {
            throw new \Exception("缺少商户单号: trade_no");
        }
        if (!isset($orderData['total_amount']) || empty($orderData['total_amount'])) {
            throw new \Exception("缺少金额: total_amount");
        }
        if (!isset($orderData['subject']) || empty($orderData['subject'])) {
            throw new \Exception("缺少订单标题: subject");
        }
        if (!isset(self::$config['app_id']) || empty(self::$config['app_id'])) {
            throw new \Exception("初始化配置中缺少应用APPID: app_id");
        }
        $apiUrl = self::$ecmUrl.'wxpay/wapPay';
        $postData['app_id'] = self::$config['app_id']; //应用APPID
        $postData['trade_no'] = $orderData['trade_no'];
        $postData['total_amount'] = $orderData['total_amount'];
        $postData['subject'] = $orderData['subject'];
        if (isset($orderData['sub_mchid'])) {
            $postData['sub_mchid'] = $orderData['sub_mchid'];
        }
        if (isset($orderData['mch_no'])) {
            $postData['mch_no'] = $orderData['mch_no'];
        }
        if (isset($orderData['sub_appid'])) {
            $postData['sub_appid'] = $orderData['sub_appid'];
        }
        if (isset($orderData['attach'])) {
            $postData['attach'] = $orderData['attach'];
        }
        if (isset($orderData['goods_detail'])) {
            $postData['goods_detail'] = $orderData['goods_detail'];
        }
        if (isset($orderData['time_expire'])) {
            $postData['time_expire'] = $orderData['time_expire'];
        }
        if (isset($orderData['invoice_id'])) {
            $postData['invoice_id'] = $orderData['invoice_id'];
        }
        if (isset($orderData['goods_tag'])) {
            $postData['goods_tag'] = $orderData['goods_tag'];
        }
        if (isset($orderData['sp_openid'])) {
            $postData['sp_openid'] = $orderData['sp_openid'];
        }
        if (isset($orderData['sub_openid'])) {
            $postData['sub_openid'] = $orderData['sub_openid'];
        }
        if (isset($orderData['profit_sharing'])) {
            $postData['profit_sharing'] = $orderData['profit_sharing'];
        }
        if (isset($orderData['cost_price'])) {
            $postData['cost_price'] = $orderData['cost_price'];
        }
        if (isset($orderData['payer_client_ip'])) {
            $postData['payer_client_ip'] = $orderData['payer_client_ip'];
        }
        if (isset($orderData['device_id'])) {
            $postData['device_id'] = $orderData['device_id'];
        }
        if (isset($orderData['store_info'])) {
            $postData['store_info'] = $orderData['store_info'];
        }
        if (isset($orderData['h5_type'])) {
            $postData['h5_type'] = $orderData['h5_type'];
        }
        if (isset($orderData['app_name'])) {
            $postData['app_name'] = $orderData['app_name'];
        }
        if (isset($orderData['app_url'])) {
            $postData['app_url'] = $orderData['app_url'];
        }
        if (isset($orderData['bundle_id'])) {
            $postData['bundle_id'] = $orderData['bundle_id'];
        }
        if (isset($orderData['package_name'])) {
            $postData['package_name'] = $orderData['package_name'];
        }
        $postData['notify_url'] = $notify_url;
        $postData['sign'] = self::$lib::createSign($postData,self::$config['appSecret']);
        $result = self::$lib::httpRequest($apiUrl, $postData);
        $result2 = json_decode($result['data'], true);
        if (empty($result2) || !isset($result2['code']) || $result2['code'] != 200) {
            throw new \Exception($result2['msg']);
        } else {
            return $result2['data'];
        }
    }
    /**
     * 付款码支付
     * @param array $orderData
     * @param string $notify_url
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4699167841791876
     */
    public static function wxpayForPaymentCode(array $orderData, string $notify_url = null)
    {
        if (!isset($orderData['trade_no']) || empty($orderData['trade_no'])) {
            throw new \Exception("缺少商户单号: trade_no");
        }
        if (!isset($orderData['total_amount']) || empty($orderData['total_amount'])) {
            throw new \Exception("缺少金额: total_amount");
        }
        if (!isset($orderData['subject']) || empty($orderData['subject'])) {
            throw new \Exception("缺少订单标题: subject");
        }
        if (!isset(self::$config['app_id']) || empty(self::$config['app_id'])) {
            throw new \Exception("初始化配置中缺少应用APPID: app_id");
        }
        $apiUrl = self::$ecmUrl.'wxpay/paymentCodePay';
        $postData['app_id'] = self::$config['app_id']; //应用APPID
        $postData['trade_no'] = $orderData['trade_no'];
        $postData['total_amount'] = $orderData['total_amount'];
        $postData['subject'] = $orderData['subject'];
        if (isset($orderData['sub_mchid'])) {
            $postData['sub_mchid'] = $orderData['sub_mchid'];
        }
        if (isset($orderData['mch_no'])) {
            $postData['mch_no'] = $orderData['mch_no'];
        }
        if (isset($orderData['sub_appid'])) {
            $postData['sub_appid'] = $orderData['sub_appid'];
        }
        if (isset($orderData['attach'])) {
            $postData['attach'] = $orderData['attach'];
        }
        if (isset($orderData['goods_detail'])) {
            $postData['goods_detail'] = $orderData['goods_detail'];
        }
        if (isset($orderData['time_expire'])) {
            $postData['time_expire'] = $orderData['time_expire'];
        }
        if (isset($orderData['invoice_id'])) {
            $postData['invoice_id'] = $orderData['invoice_id'];
        }
        if (isset($orderData['goods_tag'])) {
            $postData['goods_tag'] = $orderData['goods_tag'];
        }
        if (isset($orderData['sp_openid'])) {
            $postData['sp_openid'] = $orderData['sp_openid'];
        }
        if (isset($orderData['sub_openid'])) {
            $postData['sub_openid'] = $orderData['sub_openid'];
        }
        if (isset($orderData['profit_sharing'])) {
            $postData['profit_sharing'] = $orderData['profit_sharing'];
        }
        if (isset($orderData['cost_price'])) {
            $postData['cost_price'] = $orderData['cost_price'];
        }
        if (isset($orderData['payer_client_ip'])) {
            $postData['payer_client_ip'] = $orderData['payer_client_ip'];
        }
        if (isset($orderData['device_id'])) {
            $postData['device_id'] = $orderData['device_id'];
        }
        if (isset($orderData['store_info'])) {
            $postData['store_info'] = $orderData['store_info'];
        }
        if (isset($orderData['limit_pay'])) {
            $postData['limit_pay'] = $orderData['limit_pay'];
        }
        if (isset($orderData['receipt'])) {
            $postData['receipt'] = $orderData['receipt'];
        }
        $postData['notify_url'] = $notify_url;
        $postData['sign'] = self::$lib::createSign($postData,self::$config['appSecret']);
        $result = self::$lib::httpRequest($apiUrl, $postData);
        $result2 = json_decode($result['data'], true);
        if (empty($result2) || !isset($result2['code']) || $result2['code'] != 200) {
            throw new \Exception($result2['msg']);
        } else {
            return $result2['data'];
        }
    }
    /**
     * 微信转帐接口
     * @param array $orderData
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-02 16:52:58
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-02 16:52:58
     * @doc https://doc.qnplat.com/detail?doc=4683630058798691/4713986525622722
     */
    public static function mchTransfers(array $orderData)
    {
        if (!isset($orderData['trade_no']) || empty($orderData['trade_no'])) {
            throw new \Exception("缺少商户单号: trade_no");
        }
        if (!isset($orderData['trans_amount']) || empty($orderData['trans_amount'])) {
            throw new \Exception("缺少金额: trans_amount");
        }
        if (!isset($orderData['subject']) || empty($orderData['subject'])) {
            throw new \Exception("缺少订单标题: subject");
        }
        if (!isset($orderData['mch_no']) || empty($orderData['mch_no'])) {
            throw new \Exception("缺少普通商户的调用商户号，为WXM开头的字符串: mch_no");
        }
        if (!isset($orderData['check_name']) || empty($orderData['check_name'])) {
            throw new \Exception("缺少校验用户姓名选项: check_name");
        }
        if (!isset($orderData['client_ip']) || empty($orderData['client_ip'])) {
            throw new \Exception("缺少IP地址: client_ip");
        }
        if (!isset($orderData['openid']) || empty($orderData['openid'])) {
            throw new \Exception("缺少用户opneid: openid");
        }
        if (!isset(self::$config['app_id']) || empty(self::$config['app_id'])) {
            throw new \Exception("初始化配置中缺少应用APPID: app_id");
        }
        $apiUrl = self::$ecmUrl.'wxpay/mchTransfers';
        $postData['app_id'] = self::$config['app_id']; //应用APPID
        $postData['trade_no'] = $orderData['trade_no'];
        $postData['trans_amount'] = $orderData['trans_amount'];
        $postData['order_title'] = $orderData['subject'];
        if (isset($orderData['sub_mchid'])) {
            $postData['sub_mchid'] = $orderData['sub_mchid'];
        }
        if (isset($orderData['mch_no'])) {
            $postData['mch_no'] = $orderData['mch_no'];
        }
        if (isset($orderData['check_name'])) {
            $postData['check_name'] = $orderData['check_name'];
        }
        if (isset($orderData['sub_appid'])) {
            $postData['sub_appid'] = $orderData['sub_appid'];
        }
        if (isset($orderData['name'])) {
            $postData['name'] = $orderData['name'];
        }
        if (isset($orderData['openid'])) {
            $postData['openid'] = $orderData['openid'];
        }
        if (isset($orderData['client_ip'])) {
            $postData['client_ip'] = $orderData['client_ip'];
        }
        if (isset($orderData['scene'])) {
            $postData['scene'] = $orderData['scene'];
        }
        if (isset($orderData['brand_id'])) {
            $postData['brand_id'] = $orderData['brand_id'];
        }
        if (isset($orderData['finder_template_id'])) {
            $postData['finder_template_id'] = $orderData['finder_template_id'];
        }
        if (isset($orderData['sub_openid'])) {
            $postData['sub_openid'] = $orderData['sub_openid'];
        }
        $postData['sign'] = self::$lib::createSign($postData,self::$config['appSecret']);
        $result = self::$lib::httpRequest($apiUrl, $postData);
        $result2 = json_decode($result['data'], true);
        if (empty($result2) || !isset($result2['code']) || $result2['code'] != 200) {
            throw new \Exception($result2['msg']);
        } else {
            return $result2['data'];
        }
    }
}
